/*******************************************************************************
 * Copyright (c) 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Bartosz Michalik (bartosz.michalik@gmail.com)
 *******************************************************************************/
package org.eclipse.pde.internal.ui.editor.product;

import java.util.Arrays;
import org.eclipse.jface.viewers.*;
import org.eclipse.pde.core.IModelChangedEvent;
import org.eclipse.pde.core.IModelChangedListener;
import org.eclipse.pde.internal.core.iproduct.*;
import org.eclipse.pde.internal.ui.editor.PDEFormPage;
import org.eclipse.pde.internal.ui.editor.TreeSection;
import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

/**
 * Section for managing changes 
 * @author Bartosz Michalik
 *
 */
public class TransformManagementSection extends TreeSection {

	private TreeViewer fTreeViewer;
	private IModelChangedListener fListener;

	private class TransformationContentProvider extends DefaultContentProvider implements ITreeContentProvider {

		private ICustomizationInfo fCustomizationInfo;

		/* (non-Javadoc)
		 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
		 */
		public Object[] getChildren(Object parent) {
			if (parent instanceof String) {
				return fCustomizationInfo.getTransformsManager().getAll((String) parent);
			}
			return new Object[0];
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
		 */
		public Object getParent(Object element) {
			return null;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
		 */
		public boolean hasChildren(Object element) {
			return getChildren(element).length > 0;
		}

		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			if (newInput instanceof IProduct) {
				fCustomizationInfo = ((IProduct) newInput).getCustomizationInfo();
			}
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
		 */
		public Object[] getElements(Object inputElement) {
			ITransformationsManager manager = fCustomizationInfo.getTransformsManager();
			Object[] result = manager.getTransforms().keySet().toArray();
			Arrays.sort(result);
			return result;
		}

	}

	private class TransformationLabelProvider extends LabelProvider {

		/* (non-Javadoc)
		 * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
		 */
		public Image getImage(Object element) {
			// TODO Auto-generated method stub
			return null;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
		 */
		public String getText(Object element) {
			if (element instanceof String) {
				return (String) element;
			} else if (element instanceof IProductTransform) {
				return ((IProductTransform) element).getXPath();
			}
			return null;
		}

	}

	/**
	 * @param formPage
	 * @param parent
	 * @param style
	 * @param buttonLabels
	 */
	public TransformManagementSection(PDEFormPage page, Composite parent) {
		super(page, parent, Section.DESCRIPTION, new String[] {"Remove"});

		//		createClient(getSection(), page.getEditor().getToolkit());
	}

	/* (non-Javadoc)
	 * @see org.eclipse.pde.internal.ui.editor.PDESection#createClient(org.eclipse.ui.forms.widgets.Section, org.eclipse.ui.forms.widgets.FormToolkit)
	 */
	protected void createClient(Section section, FormToolkit toolkit) {
		section.setText("Transformations");
		GridData sectionData = new GridData(SWT.FILL, SWT.FILL, true, true);
		section.setLayoutData(sectionData);
		Composite container = createClientContainer(section, 3, toolkit);
		createViewerPartControl(container, SWT.SINGLE, 3, toolkit);

		fTreeViewer = getTreePart().getTreeViewer();
		fTreeViewer.setContentProvider(new TransformationContentProvider());
		fTreeViewer.setLabelProvider(new TransformationLabelProvider());
		fTreeViewer.setInput((getProductModel()).getProduct());
		
		fListener = new IModelChangedListener() {

			public void modelChanged(IModelChangedEvent event) {
				Object[] changedObjects = event.getChangedObjects();
				boolean refreshNeeded = false;
				for (int i = 0; i < changedObjects.length; ++i) {
					if (changedObjects[i] instanceof IProductTransform) {
						refreshNeeded = true;
						break;
					}
				}
				if (refreshNeeded) {
					fTreeViewer.setInput((getProductModel()).getProduct());
					fTreeViewer.refresh();
				}
			}

		};

		getProductModel().addModelChangedListener(fListener);

		toolkit.paintBordersFor(container);
		section.setClient(container);
	}

	/**
	 * @return
	 */
	private IProductModel getProductModel() {
		return (IProductModel) getPage().getPDEEditor().getAggregateModel();
	}

}
